/**
 * jims
 */
package com.jims.master.modules.oper.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jims.master.modules.oper.entity.LabType;
import com.jims.master.modules.oper.utils.LabTypeTreeTable;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.OrgDeptService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.oper.service.LabTypeService;

import java.util.List;
import java.util.Map;

/**
 * 检验类别Controller
 * @author 赵宁
 * @version 2017-02-24
 */
@Controller
@RequestMapping(value = "${adminPath}/oper/labType")
public class LabTypeController extends BaseController {

	@Autowired
	private LabTypeService labTypeService;
	@Autowired
	private OrgDeptService orgDeptService;
	
	@ModelAttribute
	public LabType get(@RequestParam(required=false) String id) {
		LabType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = labTypeService.get(id);
		}
		if (entity == null){
			entity = new LabType();
		}
		return entity;
	}

	/**
	 * 加载列表
	 * @param labType
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(LabType labType, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<LabType> list = labTypeService.findList(labType);
		List<LabType> treeList = new LabTypeTreeTable(list).buildTree();
		if(treeList.size()==0){
			treeList=list;
		}
		model.addAttribute("list", treeList);
		return "modules/oper/labTypeIndex";
	}

	/**
	 * 跳转到 新增修改页面
	 * @param labType
	 * @param model
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(LabType labType, Model model) {
		model.addAttribute("labType", labType);
		return "modules/oper/labTypeForm";
	}

	/**
	 * 保存 修改 信息
 	 * @param labType
	 * @param model
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(LabType labType, Model model) {
	    StringData data=new StringData();
		String orgId = UserUtils.getUser().getOrgId();
		labType.setOrgId(orgId);
		int i=labTypeService.save(labType);
        if(i>0){
            data.setCode("success");
            data.setData("保存检验类别成功");
        }else{
            data.setCode("error");
            data.setData("保存检验类别失败");
        }
        return data;
	}

	/**
	 * 删除数据
	 * @param labType
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(LabType labType) {
	    StringData data=new StringData();
		int i=labTypeService.delete(labType);
        if(i>0){
            data.setCode("success");
            data.setData("删除检验类别成功");
        }else{
            data.setCode("error");
            data.setData("删除检验类别失败");
        }
		return data;
	}
	/**
	 * 获取机构JSON数据。
	 * @param extId 排除的ID
	 * @param response
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		LabType labType=new LabType();
		User user= UserUtils.getUser();
		labType.setOrgId(user.getOrgId());
		List<LabType> list = labTypeService.findList(labType);
		for (int i=0; i<list.size(); i++){
			LabType e = list.get(i);
			if ((StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf("."+extId+".")==-1))){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("pIds", e.getParentIds());
				map.put("name", e.getName());
				map.put("deptId",e.getPerformedBy());
				OrgDept orgDept=orgDeptService.get(e.getPerformedBy());
				map.put("deptName",orgDept.getDeptName());
				mapList.add(map);
			}
		}
		return mapList;
	}
}